<?php
/**
 * Created by PhpStorm.
 * User: tt
 * Date: 2017/12/20
 * Time: 10:56
 */

namespace app\admin\controller;
use app\common\controller\Sms;
use app\common\controller\WeChatSms;
use app\common\traits\SendMessage;
use app\api\traits\Wechat;
use app\common\traits\WechatZjs;
use function GuzzleHttp\Promise\is_settled;
use think\Controller;
use think\Db;

class We extends Controller
{
    use Wechat;


    public function index(){
        $job   = 'app\queue\jobs\QueueClient@sendWechatsms';
        $datas['openid'] = 'onzolw9fmFpZlflEW2EZnn1ihsC8';
        $datas['template'] = '7f0KH_RAUUeDWyj4qtsYddePGBHkfOhcEJN5lNDaZ9I';
        $datas['url'] = "http://www.baidu.com";
        $keyword['keyword1'] = "唐冲英";
        $keyword['keyword2'] = '沙坪坝门店';
        $keyword['keyword3'] = date('Y-m-d H:i', time());
        $datas['data'] = array(
            'name'=>array('value'=>$keyword['keyword1'],'color'=>'#000'),//产品名称
            'price'=>array('value'=>$keyword['keyword2'],'color'=>'#000'),//购买数量
            'deta'=>array('value'=>$keyword['keyword3'],'color'=>'#000'),//订单金额
        );
        dump($datas['data']['name']['value']);
        \think\Queue::push($job, $datas , $queue = null);



//        $openid = "onzolw9fmFpZlflEW2EZnn1ihsC8";
//        $template = "7f0KH_RAUUeDWyj4qtsYddePGBHkfOhcEJN5lNDaZ9I";
//        $url = "http://www.baidu.com";
//        $keyword['keyword1'] = "唐冲英";
//        $keyword['keyword2'] = '沙坪坝门店';
//        $keyword['keyword3'] = date('Y-m-d H:i', time());
//        $data = array(
//            'name'=>array('value'=>$keyword['keyword1'],'color'=>'#000'),//产品名称
//            'price'=>array('value'=>$keyword['keyword2'],'color'=>'#000'),//购买数量
//            'deta'=>array('value'=>$keyword['keyword3'],'color'=>'#000'),//订单金额
//        );
        //调用发送微信模板
//        $this->sendWechat();
    }

    public function wechat(){
        $access_token = self::getInstance()->access_token->getToken();
        dump($access_token);
    }

    public function sendWechat($openid,$template,$data,$url){
//        dump(123456789);
//        $send = new WeChatSms();
//        $access_token = $this->getaccesstoken($qiangzhi = false);

        $access_token = self::getInstance()->access_token->getToken();
        dump($access_token);
        //获取动态的access_token
//        $appid = "wxee02c358351b9809";
//        $secret = "904afa5758f0bff3a3a92adeb8b8967f";
//        $urll = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
//        $wxt = json_decode(file_get_contents($urll),true);
//        $access_token = $wxt['access_token'];
//        dump($access_token);

        $wechatcon = [
            'touser'=>$openid,//openid
            'template_id'=>$template,
            'url'=>$url,
            'data'=>$data
        ];
        $params = json_encode($wechatcon,JSON_UNESCAPED_UNICODE);
        $fp = fsockopen('api.weixin.qq.com', 80, $error, $errstr, 1);
        $http = "POST /cgi-bin/message/template/send?access_token={$access_token} HTTP/1.1\r\nHost: api.weixin.qq.com\r\nContent-type: application/x-www-form-urlencoded\r\nContent-Length: " . strlen($params) . "\r\nConnection:close\r\n\r\n$params\r\n\r\n";

        //发送微信模板消息
            $res = fwrite($fp, $http);

        //微信模板日志
        $datas['send_id'] = '';
        $datas['receive_id'] = $openid;
        $datas['send_msg'] = $data['name']['value'].','.$data['price']['value'];
        $datas['log_type'] = 1;
        $datas['create_time'] = time();
        if($res){
            dump('微信模板发送成功');
            $datas['is_success'] = 1;
            Db::name('wesmslog')->insert($datas);

            return true;
        }else {
            dump('微信模板发送失败');
            $datas['is_success'] = 2;
            Db::name('wesmslog')->insert($datas);
            return false;
        }
    }





//    public function sendWechat($openid, $template, $keyword){
//        //初始化变量
//        $touser = $openid;  //微信用户ID
//        //模板消息ID
//        $template_id = $template;
//        //跳转地址链接
//        $url = 'https://shanpai.121314.com/index/order/';
//        //头部颜色
//        $topcolor = '#7B68EE';
//        //发送的内容
//        $data = array(
//            'name'=>array('value'=>urlencode($keyword['keyword1']),'color'=>'#000'),//产品名称
//            'price'=>array('value'=>urlencode($keyword['keyword2']),'color'=>'#000'),//购买数量
//            'deta'=>array('value'=>urlencode($keyword['keyword3']),'color'=>'#000'),//订单金额
//        );
//        //调用发送模板消息
//        $send = new WeChatSms();
//        $send->sendWxTemplateMessage($touser,$template_id,$url,$topcolor,$data);
//    }


    //调用短信队列模板
    public function sendMessage(){
        $job   = 'app\queue\jobs\QueueClient@sendMAIL';
        $data['signName'] = '唐冲英';  //短信签名
        $data['templateCode'] = 'SMS_117522153';  //短信模板
        $data['phoneNumbers'] = 15340365371;   //发送的电话
        \think\Queue::push($job, $data , $queue = null);


//        $signName = '唐冲英';  //短信签名
//        $templateCode = 'SMS_117522153';  //短信模板
//        $phoneNumbers = 15340365371;   //发送的电话
//        $this->message($signName,$templateCode,$phoneNumbers);
    }



    public function message($signName,$templateCode,$phoneNumbers,$optionalData){
        // 初始化SendSmsRequest实例用于设置发送短信的参数
        $Send = new Sms();
        $res = $Send::sendSms($signName,$templateCode,$phoneNumbers,$optionalData);

        //短信模板日志
        $datas['send_id'] = $phoneNumbers;
        $datas['receive_id'] = $phoneNumbers;
        $datas['send_msg'] = '好很好非常好';
        $datas['log_type'] = 2;
        $datas['create_time'] = time();
        if($res){
            dump('短信发送成功');
            $datas['is_success'] = 1;
            Db::name('wesmslog')->insert($datas);
            return $res;
        }else{
            dump('短信发送失败');
            $datas['is_success'] = 2;
            Db::name('wesmslog')->insert($datas);
            return $res;
        }

    }

}